package com.zzl.rabbitmq.ack;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zzl.rabbitmq.utils.RabbitMqUtils;

/**
 * Title: AckConsumer.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/7 11:01 <br>
 *
 * @author zhuangzhuangliang
 */
public class AckConsumer {

    public static final String QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, Boolean.FALSE, null);
        //回调函数
        DeliverCallback deliverCallback = (consumerTag, message) -> {

            //接收到的消息
            String msg = new String(message.getBody(), "UTF-8");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(msg);
            //1.消息的tag 2. true 批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        //关闭自动应答
        channel.basicConsume(QUEUE_NAME,false,deliverCallback, consumerTag ->{
        });
    }
}
